<?php
namespace app\services\index;

use app\services\RESTfulAPI;
use kernel\library\Config;
use kernel\library\Request;
use kernel\library\Es7;

class Finder extends RESTfulAPI
{
    protected function menu(array $input = []){
        $path = Request::getInstance()->path();
        $arr = explode('/', $path);
        
        $data = array (
            'id' => -1,
            'title' => $arr[3],
            'icon' => 'fa fa-circle-o',
            'api' => $path,
            'page' => '/admin/page/finder.html',
            'target' => '_self',
            'parent_id' => -1,
            'db_table' => '',
            'table_struct' =>
            array (
                'field' =>
                array (
                    '_id' => [
                        'field' =>	"_id",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"_id",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "60",
                        'input_type' => 'text',
                    ],
                    '_index' => [
                        'field' =>	"_index",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"_index",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "60",
                        'input_type' => 'text',
                    ],
                    '_type' => [
                        'field' =>	"_type",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"_type",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "60",
                        'input_type' => 'text',
                    ],
                    '_score' => [
                        'field' =>	"_score",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"_score",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "60",
                        'input_type' => 'text',
                    ],
                    '_source' => [
                        'field' =>	"_source",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"_source",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "500",
                        'input_type' => 'textarea',
                    ],
                    'sort' => [
                        'field' =>	"sort",
                        'type' =>	"varchar",
                        'null' =>   "no",
                        'default' => null,
                        'label' =>	"sort",
                        'search' =>	false,
                        'in_list' => true,
                        'in_field' => true,
                        'width' => 120,
                        'input_maxlength' => "hidden"
                    ],
                ),
                'primary_key' => '_id',
                'tree' =>[]
            ),
            'stepform_fields' => NULL,
            'head_toolbar' =>
            array (
                0 =>
                array (
                    'event' => 'add',
                    'ico' => '&#xe608;',
                    'title' => '添加',
                    'href' => '',
                ),
                1 =>
                array (
                    'event' => 'delete',
                    'ico' => '&#xe640;',
                    'title' => '删除',
                    'href' => '',
                ),
            ),
            'row_toolbar' =>
            array (
                0 =>
                array (
                    'event' => 'edit',
                    'ico' => '&#xe642;',
                    'title' => '编辑',
                    'href' => '',
                ),
                1 =>
                array (
                    'event' => 'delete',
                    'ico' => '&#xe640;',
                    'title' => '删除',
                    'href' => '',
                ),
            ),
            'operate_width' => 150,
            'custom_templist' => NULL,
            'custom_tempadd' => NULL,
            'custom_tempedit' => NULL,
            'sortrank' => 50,
            'is_basics' => 0,
            'eshost' => NULL,
            'href' => '/admin/page/finder.html?api=/admin/dbfinder/meditation_author',
            'detail' => '',
        );
        
        return $this->result($data);
    }
    protected function post(array $input = [])
    {}

    protected function get(array $input = [])
    {
        $request = Request::getInstance();
        $path = $request->path();
        $arr = explode('/', $path);
        
        //$where = $this->getWhereRaw($request, $path);
        
        //搜索的话自定义字段 和 值
        $es7 = Es7::getInstance(Config::get('app.eshost'))->index($arr[3])->_id('');
//         if(isset($where[0]) && $where[0] == '_id'){
//             $es7->term([
//                 'id' => $where[2]
//             ]);
//         }else{
//             //模糊查询
//             $es7->term([
//                 'id' => $where[2]
//             ]);
//         }
        $res = $es7->finder($input['page'], $input['limit']);
        
        return $res;
    }

    protected function getWhereRaw($request, $path){
        $cookie = $request->cookie($path.'_so');
        $arr = @json_decode($cookie, true);
        foreach($arr as $k => $v){
            if($v[2] != '' && $v[2] != '%%'){
                $v[2] = base64_decode($v[2]);
                $v[2] = urldecode($v[2]);
                $v[2] = str_replace('%', '', $v[2]);
                return $v;
            }
        }
    }
    protected function delete(array $input = [])
    {
        $arr = explode('/', Request::getInstance()->path());
        foreach($input['_id'] as $v){
            Es7::getInstance(Config::get('app.eshost'))->index($arr[3])->_id($v)->delete();
        }
        return $this->result();
    }

    protected function put(array $input = [])
    {}

}

